//require CodeRAD
//require RADChatRoom
package com.codename1.samples;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Form;
import com.codename1.rad.controllers.ApplicationController;
import com.codename1.rad.controllers.Controller;
import com.codename1.rad.controllers.FormController;
import com.codename1.rad.models.Entity;
import com.codename1.rad.models.EntityList;
import com.codename1.rad.models.EntityType;
import com.codename1.rad.nodes.ActionNode;
import com.codename1.rad.nodes.ViewNode;
import com.codename1.rad.schemas.ChatMessage;
import com.codename1.rad.schemas.ChatRoom;
import com.codename1.rad.schemas.Thing;
import static com.codename1.rad.ui.UI.action;
import static com.codename1.rad.ui.UI.actions;
import static com.codename1.rad.ui.UI.enabledCondition;
import static com.codename1.rad.ui.UI.icon;
import static com.codename1.rad.ui.UI.param;
import com.codename1.rad.ui.chatroom.ChatBubbleView;
import com.codename1.rad.ui.chatroom.ChatRoomView;
import com.codename1.rad.ui.entityviews.ProfileAvatarView;
import com.codename1.ui.CN;
import com.codename1.ui.FontImage;
import com.codename1.ui.layouts.BorderLayout;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class RADChatRoomTest3 extends ApplicationController {
    public static final ActionNode send = action(
        enabledCondition(entity-> {
            return !entity.isEmpty(ChatRoom.inputBuffer);
        }),
        icon(FontImage.MATERIAL_SEND)
    );
    
    public static final ActionNode videoConference = action(
        icon(FontImage.MATERIAL_VIDEOCAM)
    );

    @Override
    public void start() {
        super.start();
        new ChatFormController(this).show();
        
    }

    
    static class ViewModel extends Entity {
        public static final EntityType TYPE = new EntityType(){{
            string(ChatRoom.inputBuffer);
            list(EntityList.class, ChatRoom.messages);
            list(EntityList.class, ChatRoom.participants);
        }};
        {
            setEntityType(TYPE);
            set(ChatRoom.messages, new EntityList(MessageModel.TYPE, -1));
            set(ChatRoom.participants, new EntityList(ParticipantModel.TYPE, -1));
        }
        
        void addParticipant(ParticipantModel participant) {
            getEntityList(ChatRoom.participants).add(participant);
        }
    }
    
    static class MessageModel extends Entity {
        public static final EntityType TYPE = new EntityType(){{
            string(ChatMessage.text);
            date(ChatMessage.datePublished);
        }};
        {
            setEntityType(TYPE);
        }      
    }
    
    static class ParticipantModel extends Entity {
        public static final EntityType TYPE = new EntityType(){{
            string(Thing.name);
            string(Thing.thumbnailUrl);
            
        }};
        {
            setEntityType(TYPE);
        }
        
        public ParticipantModel(String name, String imageUrl) {
            set(Thing.name, name);
            set(Thing.thumbnailUrl, imageUrl);
        }
    }
    
    static class ChatFormController extends FormController {
        
        public ChatFormController(Controller parent) {
            super(parent);
            Form f = new Form("Chat Room", new BorderLayout());
            ViewModel model = new ViewModel();
            
            for (int i=0; i< 100; i++) {
                MessageModel msg = new MessageModel();
                msg.setText(ChatMessage.text, "Hello "+i);
                msg.setDate(ChatMessage.datePublished, new Date());
                model.getEntityList(ChatRoom.messages).add(msg);
            }
            
            MessageModel lastMessage = new MessageModel();
            lastMessage.setText(ChatMessage.text, "This is the one to scroll to");
            lastMessage.setDate(ChatMessage.datePublished, new Date());
            model.getEntityList(ChatRoom.messages).add(lastMessage);
            
            model.addParticipant(new ParticipantModel("George", "https://weblite.ca/cn1tests/radchat/george.jpg"));
            model.addParticipant(new ParticipantModel("Kramer", "https://weblite.ca/cn1tests/radchat/kramer.jpg"));
            ChatRoomView chatView = new ChatRoomView(model, createViewNode(), f);
            f.add(CENTER, chatView);
            setView(f);
            f.addShowListener(evt->{
                chatView.scrollToMessage(lastMessage);
            });
            
            
            addActionListener(send, evt->{
                MessageModel msg = new MessageModel();
                msg.set(ChatMessage.text, evt.getEntity().getText(ChatRoom.inputBuffer));
                msg.setDate(ChatMessage.datePublished, new Date());
                evt.getEntity().getEntityList(ChatRoom.messages).add(msg);
                evt.getEntity().setText(ChatRoom.inputBuffer, "");
            });
            
        }

        @Override
        protected ViewNode createViewNode() {
            return new ViewNode(
                    //Uncomment to prevent use of overflow container (swipe left)
                    // to see the time of a chat bubble.
                    // param(ChatBubbleView.USE_OVERFLOW_CONTAINER, false),
                    
                    // Add "send" action so that the user can post messages
                    actions(ChatRoomView.SEND_ACTION, send),
                    
                    // Add action to appear in the menu when a user avatar is clicked
                    actions(ProfileAvatarView.PROFILE_AVATAR_CLICKED_MENU, videoConference)
                    
            );
        }
        
        

    }

}
